package cn.ren.cloud.properties;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author 23508
 */
@Data
@Component
@ConfigurationProperties("alipay")
public class AliPayProperties {

    private String protocol;
    private String gatewayHost;
    private String signType;
    private String appId;
    private String merchantPrivateKey;
    private String alipayPublicKey;
    private String notifyUrl;

    /**
     * 此注解可以让项目启动时，方法执行一次
     */
    @PostConstruct
    public void init(){
        // 1.初始化参数
        Config config = new Config();
        config.protocol = this.protocol;
        config.gatewayHost = this.gatewayHost;
        config.signType = this.signType;
        config.appId = this.appId;
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = this.merchantPrivateKey;
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = this.alipayPublicKey;
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = this.notifyUrl;
        // 2. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
    }


}
